<?php
// +----------------------------------------------------------------------
// | 99PHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2020 https://www.99php.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Mr.Chung <chung@99php.cn >
// +----------------------------------------------------------------------

namespace app\common\service;


use think\Model;

/**
 * 模型基础数据服务
 * Class ModelService
 * @package service
 */
class ModelService extends Model
{

    /**
     * 主键定义
     * @var string
     */
    protected $pk = 'id';

    /**
     * 查询多条数据
     * @param array $where 条件
     * @param string $field 查询字段
     * @param int $nowPage 当前页数
     * @param int $limits 每页条数
     * @param array $join 连表
     * @param string $order 排序
     * @param string $alias 表别名
     * @return
     */
    public function getAll(array $where = [], string $field = '*', int $nowPage = 1, int $limits = -1, array $join = [], $order = '', $group = '', $alias = 'a')
    {
        if ($limits < 0) {
            if (!empty($group)) {
                $result = $this->alias($alias)
                    ->where($where)
                    ->join($join)
                    ->field($field)
                    ->order($order)
                    ->group($group)
                    ->select();
            } else {
                $result = $this->alias($alias)
                    ->where($where)
                    ->join($join)
                    ->field($field)
                    ->order($order)
                    ->select();
            }

        } else {
            if (!empty($group)) {
                $result = $this->alias($alias)
                    ->where($where)
                    ->join($join)
                    ->field($field)
                    ->page($nowPage, $limits)
                    ->order($order)
                    ->group($group)
                    ->select();
            } else {
                $result = $this->alias($alias)
                    ->where($where)
                    ->join($join)
                    ->page($nowPage, $limits)
                    ->field($field)
                    ->order($order)
                    ->select();
            }

        }
        //dump($this->getLastSql($result));
        return $result;
    }

    /**
     * 统计数量
     * @param $where 查询条件
     * @param array $join 连表
     * @param string $alias 表别名
     * @return int|string
     * @throws
     */
    public function getCount(array $where = [], array $join = [], $alias = 'a')
    {
        $result = self::alias($alias)->where($where)->join($join)->count();
        return $result;
    }

    /**
     * 查询单条信息
     * @param $where 查询条件
     * @param string $field 查询字段
     * @param array $join 连表
     * @param string $alias 表别名
     * @return
     */
    public function getOne(array $where, string $field = '*', array $join = [], $alias = 'a')
    {
        $result = self::alias($alias)
            ->where($where)
            ->join($join)
            ->field($field)
            ->find();
        return $result;
    }


    /**
     * 批量插入
     * @param array $data 数据
     * @return int|string
     */
    public function addAll(array $data = [])
    {
        return $this->insertAll($data);
    }


    /**
     * 单条插入
     * @param $data 数据
     * @return int|string 返回主键ID
     */
    public function add(array $data)
    {
        return self::insertGetId($data);
    }

    /**
     * 修改
     * @param array $where 修改条件
     * @param array $data 数据
     * @return ModelService
     */
    public function edit(array $where = [], array $data = [])
    {
        return self::where($where)->update($data);
    }

    /**
     * 物理删除
     * @param array $where 删除条件
     * @return int
     */
    public function del(array $where = [])
    {
        return self::where($where)->delete();
    }

    /**
     * 查询单个字段
     * @param $where 查询条件
     * @param $field 字段名
     * @return mixed
     */
    public function getValue(array $where, string $field)
    {
        return self::where($where)->value($field);
    }

    /**
     * 查询某一列
     * @param $where 查询条件
     * @param $field 字段名
     * @param array $join 连表
     * @param string $alias 表别名
     * @return array
     */
    public function getColum(array $where, string $field, array $join = [], $alias = 'a')
    {
        return self::alias($alias)->where($where)->join($join)->column($field);
    }


    /**
     * 计算某一列的总和
     * @param array $where 条件
     * @param array $join 连表
     * @param string $field 字段名
     * @param string $alias 表别名
     * @return float|int
     */
    public function getSum(array $where = [], array $join = [], string $field = '*', $alias = 'a')
    {
        return self::alias($alias)->where($where)->join($join)->sum($field);
    }

    /**
     * 单个字段值加
     * @param array $where
     * @param string $field
     * @param int $num
     * @return bool|int|true
     * @throws \think\Exception
     */
    public function setInc($where = [], $field = '', $num = 0)
    {
        return self::where($where)->setInc($field, $num);

    }

    /**
     * 单个字段值减
     * @param array $where
     * @param string $field
     * @param int $num
     * @return bool|int|true
     * @throws \think\Exception
     */
    public function setDec($where = [], $field = '', $num = 0)
    {
        return self::where($where)->setDec($field, $num);

    }

}